#include "../App.hpp"
#include "GameState.hpp"

GameState::GameState(App* appPtr) : AppState("GameState", appPtr)
{
    m_view.SetCenter(400,256);
    m_view.SetHalfSize(400,256);
    m_app->m_window.SetView(m_view);
    m_app->m_window.EnableKeyRepeat(1);
}

GameState::~GameState()
{
    delete m_level;
    delete m_player;
}

void GameState::DoInit()
{
	m_level = new Level(m_app);

	m_level->Load("./level/maps/map001.map");

	m_physic.Loading("./level/maps/map001.map");

	m_player = new Player(m_app,&m_physic);

	m_player->SetPosition(m_level->SpawnPosition());

    m_hub = new Hub(m_app,&m_player->m_life,&m_player->m_banana);

    m_hub->Loading();

    m_hub->SetTitle("Test Map");
}

void GameState::ReInit()
{

	m_view.SetCenter(400,256);
    m_view.SetHalfSize(400,256);
    m_app->m_window.SetView(m_view);
}

void GameState::Update(float elapsedTime)
{
	 m_player->Input(m_app->m_window.GetInput());
	 m_player->Update(elapsedTime);

     m_hub->UpdateHub(elapsedTime);

     for(int i = 0; i < m_level->Items().size(); i++)
         m_level->Items().at(i)->GetAnimation()->UpdateAnim(elapsedTime);
}


void GameState::HandleEvents(sf::Event appEvent)
{

	if(appEvent.Type == sf::Event::KeyPressed)
	{
		switch(appEvent.Key.Code)
		{
		case sf::Key::Escape:
            m_app->ChangeResolution(600,400);
            m_app->m_window.Create(sf::VideoMode(600, 400), GAME_TITLE);
            m_app->m_stateManager.SetActiveState("MenuState");
		break;
		}
	}
}


void GameState::Draw()
{
    //m_level->Scrolling((m_view.GetCenter().x - m_view.GetHalfSize().x),(m_level->Map()->Size().x - m_app->m_window.GetWidth()));

    // Draw Backgrounds
	for(int i = 0; i < m_level->Backgrounds().size(); i++)
	{
	    m_app->m_window.Draw(*(m_level->Backgrounds().at(i)));
	}


	//Draw Map
	m_app->m_window.Draw(*(m_level->Map()));

    //Draw Donkey kong and Diddy Kong
    m_app->m_window.Draw(m_player->DrawCharacter());
    //m_app->m_window.Draw(m_player->DrawSecondCharacter());    //Not operational for this time

	// Draw Foregrounds
	for(int i = 0; i < m_level->Foregrounds().size(); i++)
		m_app->m_window.Draw(*(m_level->Foregrounds().at(i)));

    // Draw Item
    for(int i = 0; i < m_level->Items().size(); i++)
        m_app->m_window.Draw(*(m_level->Items().at(i)->GetAnimation()));

    // Draw Hub
    for(int i = 0; i <= 5;i++)
        m_app->m_window.Draw(m_hub->DrawHub(i));

    //Draw Title during 5sec
    m_app->m_window.Draw(m_hub->DrawTitle());
}

void GameState::CleanUp()
{

}
